import 'package:get/get.dart';
import 'package:nyh_motorcade_app/constant/constant.dart';
import 'package:nyh_motorcade_app/entity/company_motorcade_card_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/utils/sp_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SelectTransferAccountController extends GetxController {
  RefreshController refreshController = RefreshController();
  MultiState multiState = MultiState.loading;
  List<AccountType> accountTypeList = [];
  String accountType = "";
  String accountTypeName = "";
  bool showAccountTypeMenu = false;
  List<CompanyMotorcadeCardList> companyMotorcadeCardList = [];

  @override
  void onReady() {
    super.onReady();
    init();
  }

  void onRetry() {
    multiState = MultiState.loading;
    update();
    getCompanyMotorcadeCardList();
  }

  void goToSearchAccountPage() {}

  Future<void> getCompanyMotorcadeCardList() async {
    httpManager.post(
      Get.context,
      HttpAddress.GET_COMPANY_MOTORCADE_CARD_LIST,
      {"superId": await SPUtils.getString(SPUtils.COMPANY_ID)},
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          multiState = MultiState.fail;
          update();
          return;
        }
        CompanyMotorcadeCardEntity entity = CompanyMotorcadeCardEntity().fromJson(data);
        switch (accountType) {
          case "COMPANY":
            companyMotorcadeCardList = entity.companyList ?? [];
            break;
          case "MOTORCADE":
            companyMotorcadeCardList = entity.motorcadeList ?? [];
            break;
          case "CARD":
            companyMotorcadeCardList = entity.cardList ?? [];
            break;
          default:
            companyMotorcadeCardList.clear();
            accountTypeList.forEach((element) {
              switch (element.type) {
                case "COMPANY":
                  List<CompanyMotorcadeCardList> list = entity.companyList ?? [];
                  if (list.isNotEmpty) {
                    companyMotorcadeCardList.addAll(list);
                  }
                  break;
                case "MOTORCADE":
                  List<CompanyMotorcadeCardList> list = entity.motorcadeList ?? [];
                  if (list.isNotEmpty) {
                    companyMotorcadeCardList.addAll(list);
                  }
                  break;
                case "CARD":
                  List<CompanyMotorcadeCardList> list = entity.cardList ?? [];
                  if (list.isNotEmpty) {
                    companyMotorcadeCardList.addAll(list);
                  }
                  break;
                default:
              }
            });
        }
        multiState = companyMotorcadeCardList.isNotEmpty ? MultiState.success : MultiState.empty;
        update();
      },
      onFail: (code, msg) {
        multiState = MultiState.fail;
        update();
      },
      onError: (msg) {
        multiState = MultiState.error;
        update();
      },
    );
  }

  void setSelectAccountType(int? value) {
    showAccountTypeMenu = false;
    if (value != null) {
      String type = accountTypeList[value].type;
      accountTypeName = accountTypeList[value].name;
      if (accountType != type) {
        accountType = type;
        multiState = MultiState.loading;
        getCompanyMotorcadeCardList();
      }
    }
    update();
  }

  void initAccountTypeList() {}

  Future<void> init() async {
    //是不是拨入
    bool isTransferIn = Get.arguments ?? false;

    bool permissionTransferCompany = await SPUtils.getBool(SPUtils.PERMISSION_TRANSFER_COMPANY);
    bool permissionTransferMotorcade = await SPUtils.getBool(SPUtils.PERMISSION_TRANSFER_MOTORCADE);
    bool permissionTransferCard = await SPUtils.getBool(SPUtils.PERMISSION_TRANSFER_CARD);

    print("公司划拨权限:$permissionTransferCompany,车队划拨权限:$permissionTransferMotorcade,卡划拨权限:$permissionTransferCard");

    accountTypeList.add(AccountType("全部账户", ""));
    if (isTransferIn) {
      if (permissionTransferCompany) {
        accountTypeList.add(AccountType("公司账户", Constant.COMPANY));
      }
      if (permissionTransferMotorcade) {
        accountTypeList.add(AccountType("车队账户", Constant.MOTORCADE));
      }
      if (permissionTransferCard) {
        accountTypeList.add(AccountType("卡账户", Constant.CARD));
      }
    } else {
      if (permissionTransferMotorcade || permissionTransferCard) {
        accountTypeList.add(AccountType("公司账户", Constant.COMPANY));
      }
      if (permissionTransferMotorcade) {
        accountTypeList.add(AccountType("车队账户", Constant.MOTORCADE));
      }
      if (permissionTransferCard) {
        accountTypeList.add(AccountType("卡账户", Constant.CARD));
      }
    }
    accountType = accountTypeList.first.type;
    accountTypeName = accountTypeList.first.name;
    update();
    getCompanyMotorcadeCardList();
  }
}

class AccountType {
  String name;
  String type;

  AccountType(this.name, this.type);
}
